package com.hema.auction.fragment;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hema.auction.R;
import com.hema.auction.activity.MessageDescActivity;
import com.hema.auction.adapter.MessageAdapter;
import com.hema.auction.base.BaseFragment;
import com.hema.auction.bean.MessageInfo;
import com.hema.auction.http.HttpTag;
import com.hema.auction.http.biz.HttpManager;
import com.hema.auction.utils.Constant;
import com.hema.auction.widget.view.EmptyView;
import com.hema.auction.widget.view.RefreshRecyclerView;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

/**
 * author by john
 */

public class MessageFragment extends BaseFragment {


    @BindView(R.id.refresh_recycler_view)
    RefreshRecyclerView refreshRecyclerView;
    private MessageAdapter messageAdapter;
    private List<MessageInfo> messageInfos = new ArrayList<>();
    private int type;

    public static MessageFragment newInstance(int type) {
        MessageFragment f = new MessageFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        f.setArguments(bundle);
        return f;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_message;
    }

    @Override
    protected void init() {
        super.init();
        type = getArguments().getInt("type");
        refreshRecyclerView.setVerLayoutManager();
        messageAdapter = new MessageAdapter(messageInfos);
        messageAdapter.setType(type);
        messageAdapter.setOnItemClickListener(this);
        messageAdapter.setEmptyView(new EmptyView(getActivity()));
        refreshRecyclerView.setAdapter(messageAdapter);
        refreshRecyclerView.setOnRefreshListener(this);
        refreshRecyclerView.setRefresh(true);
        refreshRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity())
                .colorResId(R.color.colorDivider).marginProvider(messageAdapter).showLastDivider()
                .size(2).build());

    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        switch (type) {
            case 0:
                HttpManager.getInstance(getActivity()).getUserMessage(this);
                break;
            case 1:
                HttpManager.getInstance(getActivity()).getNotice(this);
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (type == 0) {
            HttpManager.getInstance(getActivity()).readMessage(messageAdapter.getItem(position).getId(), this);
            messageAdapter.getData().get(position).setIsRead(1);
            messageAdapter.notifyItemChanged(position);
        }
        Bundle bundle = new Bundle();
        bundle.putParcelable(Constant.EXTRA_MESSAGE_INFO, messageAdapter.getData().get(position));
        intentTo(MessageDescActivity.class, bundle);
    }

    @Override
    protected void onSuccess(HttpTag httpTag, Call call, JSONObject result) {
        super.onSuccess(httpTag, call, result);
        switch (httpTag) {
            case TAG_GET_USER_MESSAGE:
            case TAG_GET_NOTICE:
                try {
                    if (result.getInt("errcode") == 0) {
                        List<MessageInfo> newMessages = new Gson().fromJson(result.getJSONArray("data").toString(), new TypeToken<List<MessageInfo>>() {
                        }.getType());
                        messageAdapter.setNewData(newMessages);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    protected void onFinish(HttpTag httpTag) {
        super.onFinish(httpTag);
        refreshRecyclerView.setRefresh(false);
    }
}

